package com.nl.controller.xuyi.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.web.bean.XuYiStation;
import com.nl.controller.web.mapper.SmsAreaMapper;
import com.nl.controller.web.service.SmsInterfacesService;
import com.nl.controller.xuyi.mapper.XuYiRegionMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class XuYiRegionService {
	@Autowired
	SmsInterfacesService smsInterfacesService;
	@Autowired
	XuYiRegionMapper xuYiRegionMapper;
	
	@Autowired
	SmsAreaMapper smsAreaMapper;
	
	@Autowired
	XuYiSubStationService xuYiSubStationService;

	public List<String> findRegionNameByPath(String pathName) {
		List<String> regionNameList = new ArrayList<String>();
		try {
			regionNameList = smsAreaMapper.findXuYiSubStationNames(pathName);
		} catch (Exception e) {
			log.info("根据路线名查询已关联点位名信息发生异常");
			e.printStackTrace();
		}
		return regionNameList;
	}


	public PageInfo<XuYiStation> findRegionNameList(int page, int limit, String regionName) {
		PageHelper.startPage(page, limit);
		List<XuYiStation> xuYiRegionList = new ArrayList<XuYiStation>();
		xuYiRegionList= xuYiRegionMapper.findRegionNameList(regionName);
		PageInfo<XuYiStation> pageInfo = new PageInfo<XuYiStation>(xuYiRegionList);
		return pageInfo;
	}


	public int regionModify(String regionName, String lnglatList,String operatorTel) {
		
		
		
		String[] lngLats = lnglatList.split("\\|");
		String lng="";
		String lat="";
		String[] lnglat;
		int result = 0 ;
		int count=0;
		
		List<String> pathNameList = xuYiRegionMapper.findPathByRegion(regionName);
		
		
		
		for(String pathName : pathNameList){
			
			int deleteResult = xuYiSubStationService.deleteSubStation(pathName, regionName);
			
			for(int i=0;i<lngLats.length;i++){
				lnglat=lngLats[i].split(",");
				lng = lnglat[0];
				lat = lnglat[1];
				result = xuYiSubStationService.subStationAdd(pathName,regionName, lng,lat, "", operatorTel);
				if(result>0){
					count++;
				}
			}
		}
		
		
		return count;
	}

}
